<?php 
class GoodsModel extends Model{
	public $table='goods';
	//商品相册模型
	private $goodsGallery;
	//商品属性模型
	private $goodsAttr;

	//自动验证商品名称不能为空
	public $validate=array(
		array('goods_name','nonull','商品名称不能为空',2,3)
	);

	//自动完成
	public $auto=array(
		array('sale_time','strtotime','function',2,3)
	);

	public function __init(){
		//相册模型
		$this->goodsGallery=K('GoodsGallery');
		//属性模型
		$this->goodsAttr=K('GoodsAttr');
	}

	public function getList(){
		$data=M('goods')->all();
		return $data;
	}

	//商品添加
	public function addGoods(){
		if($this->create()){
			if($gid=$this->add()){
				//更新货号
				$this->setGoodsSn($gid);
				//设置商品附表
				$_POST['goods_gid']=$gid;
				M('goods_data')->add();
				//处理商品图片
				$this->setGoodsImage($gid);
				//添加商品相册(详情页图片信息)操作goods_gallery表
				$this->goodsGallery->addImage($gid);

				//商品属性(操作商品属性表goods_attr)
				$this->goodsAttr->addAttr($gid);
				return true;
			}else{
				$this->error='商品添加失败';
			}
		}

	}

	//商品编辑
	public function editGoods(){
		if($this->create()){
			//商品ID
			$gid=Q('gid',0,'intval');
			if($this->save()){
				//设置货号
				$this->setGoodsSn($gid);
				//修改附表数据
				M('goods_data')->where("goods_gid={$gid}")->save();
				//处理商品图片
				$this->setGoodsImage($gid);
				//添加商品相册(详情页图片信息)操作goods_gallery表
				$this->goodsGallery->addImage($gid);
				//添加商品属性
				$this->goodsAttr->addAttr($gid);
				return true;
			}else{
				$this->error='商品编辑失败';
			}

		}
	}

	//删除商品
	public function delGoods(){
		$gid=Q('gid',0,'intval');
		$goodsdata=$this->find($gid);

		is_file($goodsdata['original_img']) and unlink($goodsdata['original_img']);
		is_file($goodsdata['goods_img']) and unlink($goodsdata['goods_img']);
		is_file($goodsdata['goods_thumb']) and unlink($goodsdata['goods_thumb']);
		if($this->del($gid)){

			M('goods_data')->del($gid);
			$imgdata=M('goods_gallery')->find($gid);
			//删详情表的原图
			is_file($imgdata['img_original']) and unlink($imgdata['img_original']);
			//删除详情表的中图
			is_file($imgdata['img_url']) and unlink($imgdata['img_url']);
			//删除小图
			is_file($imgdata['thumb_url']) and unlink($imgdata['thumb_url']);
			M('goods_gallery')->where("goods_gid=$gid")->del();

			M('goods_attr')->where("goods_gid=$gid")->del();
			return true;
		}else{
			$this->error='商品删除失败';
		}
	}

	//设置货号，未填写货号，系统根据商品ID自动分配
	private function setGoodsSn($gid){
		if(empty($_POST['goods_sn'])){
			$goods_sn='hd_'.str_pad($gid,6,0,STR_PAD_LEFT);
			$data=array(
				'gid'=>$gid,
				'goods_sn'=>$goods_sn
			);
			M('goods')->save($data);
		}
	}

	//处理商品图片
	public function setGoodsImage($gid){
		//上传图片类处理上传图片
		$upload=new Upload('Upload/goods/'.date('Y/m/d'));
		$file=$upload->upload('original_img');
		//没有图片上传时
		if(empty($file)){
			return ;
		}
		$file=$file[0];
		//生成首页图片
		$listImg=$file['dir'].$file['filename'].'_list_300X300.'.$file['ext'];
		$indexImg=$file['dir'].$file['filename'].'_index_220X330.'.$file['ext'];
		//通过缩略图类得到中图、小图
		$image=new Image();
		$image->thumb($file['path'],$listImg,300,300,6);
		$image->thumb($file['path'],$indexImg,220,330,6);
		$data=array(
			'gid'=>$gid,  //商品ID
			'original_img'=>$file['path'],  //原图
			'goods_img'=>$listImg,   //商品图片
			'goods_thumb'=>$indexImg,  //缩略图
		);
		return M('goods')->save($data);
	}

	
}


?>